﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HanGu.Common;
using HanGu.Entity;

namespace HanGu.CRM.Areas.Dictionary.Controllers
{
    public class DefaultController : AdminBaseController
    {
        public ActionResult Index()
        {
            return View(GetDictionaryList());
        }

        [HttpPost]
        public ActionResult Index(FormCollection collection)
        {

            SearchParma.Pagination.RowCount = null;
            return View(GetDictionaryList());
        }

        List<HanGu.Entity.Dictionary> GetDictionaryList()
        {
            List<HanGu.Entity.Dictionary> dictionaryList = serviceDictionary.GetAll(base.SearchParma);
            ViewData["RowCount"] = SearchParma.Pagination.RowCount;
            return dictionaryList;
        }

        public ActionResult Create(string id, string guid)
        {
            HanGu.Entity.Dictionary dictionary = new HanGu.Entity.Dictionary();
            if (!string.IsNullOrEmpty(id) && id != "0")
            {
                dictionary = serviceDictionary.GetByID(id);
            }
            ViewData["type"] = string.IsNullOrEmpty(id) || id == "0";
            return View(dictionary);
        }

        //
        // POST: /Dictionary/Create

        [HttpPost]
        [ValidateInput(false)]
        public string Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here
                HanGu.Entity.Dictionary dictionary = new HanGu.Entity.Dictionary();
                TryUpdateModel(dictionary, collection);
                bool result = false;
                dictionary.GenerateKey = string.Format("{0}_{1}", dictionary.GroupName, dictionary.key);
                if (Request["type"] != null && Request["type"].ToLower() == "true")
                {
                    if (serviceDictionary.IsExits(dictionary.GroupName, dictionary.key))
                    {
                        ajaxStr = string.Format(ajaxStr, 300, ResourceManager.DictExitsed, navTabId, "", "");
                        return "{" + ajaxStr + "}";
                    }
                    else
                    {
                        result = serviceDictionary.Add(dictionary);
                    }
                }
                else
                {
                    result = serviceDictionary.Update(dictionary);
                }

                if (result)
                {
                    ajaxStr = string.Format(ajaxStr, 200, 
                        ResourceManager.SaveSuccess, 
                        navTabId, 
                        "",
                        collection["IsContinue"] != null && collection["IsContinue"] == "False" ?  "closeCurrent":string.Empty);
                }
                else
                {
                    ajaxStr = string.Format(ajaxStr, 300, ResourceManager.SaveFail, navTabId, "", "");
                }

            }
            catch (Exception ex)
            {
                base.ExceptionHand(ex);
                ajaxStr = string.Format(ajaxStr, 300, ResourceManager.SaveFail, navTabId, "", "");
            }
            return "{" + ajaxStr + "}";
        }

        //
        // GET: /Dictionary/Delete/5
        [HttpPost]
        public string Delete(string id, string guid)
        {
            try
            {
                if (serviceDictionary.Delete(id, guid))
                {
                    ajaxStr = string.Format(ajaxStr, 200, ResourceManager.DeleteSuccess, navTabId, "", "");
                }
                else
                {
                    ajaxStr = string.Format(ajaxStr, 300, ResourceManager.DeleteFail, navTabId, "", "");
                }
            }
            catch (Exception ex)
            {
                ExceptionHand(ex);
                ajaxStr = string.Format(ajaxStr, 300, ResourceManager.DeleteFail, navTabId, "", "");
            }

            return "{" + ajaxStr + "}";

        }
    }
}
